package com.onemile.controls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.onemile.app.R;
import com.onemile.entity.model.EAttribute;
import com.onemile.entity.model.EAttributeDetails;
import com.onemile.entity.model.ECommodityAccessories;
import com.onemile.entity.model.ESpecification;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class AdpLV_DialogMultiSelect extends BaseAdapter
{
   private ArrayList<?> mList;
   private Context mContext;
   private LayoutInflater mInflater;

   private HashMap<Integer, Boolean> IsSelectMap;
  
   public AdpLV_DialogMultiSelect(ArrayList<?> list, Context context)
   {
	  mList = list;
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
	  IsSelectMap=new HashMap<Integer, Boolean>();
	  initDate();
   }

   // 初始化isSelectedMap的数据
   private void initDate()
   {
	  for (int i = 0; i < mList.size(); i++)
	  {
		 SetSelect(i, false);
	  }
   }
   
   /**
    * 设置该行的选中状态
    * 
    * @param position
    * @param isSelected
    */
   public void SetSelect(int position, boolean isSelected)
   {
	  IsSelectMap.put(position, isSelected);
   }
   
   /**
    * 获取该行的选中状态
    * 
    * @param postion
    * @return
    */
   public Boolean GetSelectState(int position)
   {
	  return IsSelectMap.get(position);
   }

   
   @Override
   public int getCount()
   {
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int position)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent)
   {
	  ViewHolder holder = null;
	  if (convertView == null)
	  {
		 holder = new ViewHolder();
		 convertView = mInflater.inflate(R.layout.dialog_multi_select_item, null);
         holder.txtItem = (TextView) convertView.findViewById(R.id.txtItem_dialog_multi_item);
		 holder.ckbItem = (CheckBox) convertView.findViewById(R.id.ckbItem_dialog_multi_item);
       		
		 Object item=mList.get(position);
		 if(item instanceof ECommodityAccessories)
		 {
			holder.txtItem.setText(((ECommodityAccessories)item).getCommodityAccessoriesName());
		 }
         
   	     holder.ckbItem.setChecked(GetSelectState(position));
   	     
//      if(GetSelectState(position))
//      {
//    	   convertView.setBackgroundResource(R.color.DarkGolden);
//      }
//      else 
//      {
//    	    convertView.setBackgroundResource(R.color.White);
//	    }
   	     convertView.setTag(holder);
	  }
	  else
	  {
		 holder = (ViewHolder) convertView.getTag();
	  }
	  return convertView;
    }
   
   public class ViewHolder
   {
	  TextView txtItem;
	  public CheckBox ckbItem;
   }

}
